public class ObjectOrientedProgramming {
    public static void main(String[] args) {
        System.out.println("=== Java 面向对象编程示例 ===\n");
        
        // 4.1 类和对象 示例
        demonstrateClassesAndObjects();
        
        // 4.2 封装 示例
        demonstrateEncapsulation();
        
        // 4.3 继承 示例
        demonstrateInheritance();
        
        // 4.4 多态 示例
        demonstratePolymorphism();
        
        // 4.5 抽象类和接口 示例
        demonstrateAbstractClassesAndInterfaces();
    }
    
    // 4.1 类和对象 示例
    public static void demonstrateClassesAndObjects() {
        System.out.println("=== 4.1 类和对象 ===");
        
        // 创建对象实例
        Person person1 = new Person("张三", 25);
        Person person2 = new Person("李四", 30);
        
        // 访问成员变量和方法
        System.out.println("person1 名称: " + person1.getName());
        System.out.println("person2 年龄: " + person2.getAge());
        
        // 使用this关键字
        person1.setIntroduction();
        
        // 使用static关键字
        System.out.println("人类数量: " + Person.getPopulation());
        
        System.out.println();
    }
    
    // 4.2 封装 示例
    public static void demonstrateEncapsulation() {
        System.out.println("=== 4.2 封装 ===");
        
        BankAccount account = new BankAccount("123456", 1000.0);
        
        // 通过getter方法访问私有变量
        System.out.println("账户号码: " + account.getAccountNumber());
        System.out.println("初始余额: " + account.getBalance());
        
        // 通过setter方法修改私有变量
        account.deposit(500.0);
        System.out.println("存款后余额: " + account.getBalance());
        
        account.withdraw(200.0);
        System.out.println("取款后余额: " + account.getBalance());
        
        System.out.println();
    }
    
    // 4.3 继承 示例
    public static void demonstrateInheritance() {
        System.out.println("=== 4.3 继承 ===");
        
        // 创建子类对象
        Student student = new Student("王五", 20, "S001");
        Teacher teacher = new Teacher("赵六", 35, "Math");
        
        // 调用继承的方法
        System.out.println("学生信息: " + student.getIntroduction());
        System.out.println("教师信息: " + teacher.getIntroduction());
        
        // 调用重写的方法
        student.study();
        teacher.teach();
        
        // 调用父类构造方法
        System.out.println("学生ID: " + student.getStudentId());
        System.out.println("教师科目: " + teacher.getSubject());
        
        System.out.println();
    }
    
    // 4.4 多态 示例
    public static void demonstratePolymorphism() {
        System.out.println("=== 4.4 多态 ===");
        
        // 方法重载示例
        Calculator calc = new Calculator();
        System.out.println("加法 (整数): " + calc.add(5, 3));
        System.out.println("加法 (浮点数): " + calc.add(5.5, 3.2));
        System.out.println("加法 (三个数): " + calc.add(5, 3, 2));
        
        // 向上转型示例
        Animal animal1 = new Dog();  // 向上转型
        Animal animal2 = new Cat();  // 向上转型
        
        animal1.makeSound();  // 调用重写的方法
        animal2.makeSound();  // 调用重写的方法
        
        // 向下转型示例
        if (animal1 instanceof Dog) {
            Dog dog = (Dog) animal1;  // 向下转型
            dog.wagTail();
        }
        
        System.out.println();
    }
    
    // 4.5 抽象类和接口 示例
    public static void demonstrateAbstractClassesAndInterfaces() {
        System.out.println("=== 4.5 抽象类和接口 ===");
        
        // 抽象类示例
        Shape circle = new Circle(5.0);
        Shape rectangle = new Rectangle(4.0, 6.0);
        
        System.out.println("圆形面积: " + circle.calculateArea());
        System.out.println("矩形面积: " + rectangle.calculateArea());
        
        // 接口示例
        Flyable bird = new Bird();
        Flyable airplane = new Airplane();
        
        bird.fly();
        airplane.fly();
        
        // Java 8+ 接口默认方法和静态方法
        Car car = new Car();
        car.move();      // 默认方法
        car.speedUp();   // 抽象方法
        
        System.out.println();
    }
}

// 4.1 类和对象 - Person类
class Person {
    // 成员变量
    private String name;
    private int age;
    private static int population = 0; // static变量
    
    // 构造方法
    public Person() {
        this.population++;
    }
    
    public Person(String name, int age) {
        this(); // 调用无参构造方法
        this.name = name; // this关键字
        this.age = age;
    }
    
    // 成员方法
    public String getName() {
        return name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setIntroduction() {
        System.out.println("我是 " + this.name + "，今年 " + this.age + " 岁");
    }
    
    public String getIntroduction() {
        return "我是 " + this.name + "，今年 " + this.age + " 岁";
    }
    
    // static方法
    public static int getPopulation() {
        return population;
    }
}

// 4.2 封装 - BankAccount类
class BankAccount {
    // 私有成员变量
    private String accountNumber;
    private double balance;
    
    // 构造方法
    public BankAccount(String accountNumber, double initialBalance) {
        this.accountNumber = accountNumber;
        this.balance = initialBalance;
    }
    
    // Getter方法
    public String getAccountNumber() {
        return accountNumber;
    }
    
    public double getBalance() {
        return balance;
    }
    
    // 业务方法
    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }
    
    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
        }
    }
}

// 4.3 继承 - Student类
class Student extends Person {
    private String studentId;
    
    public Student(String name, int age, String studentId) {
        super(name, age); // 调用父类构造方法
        this.studentId = studentId;
    }
    
    // 方法重写
    @Override
    public String getIntroduction() {
        return super.getIntroduction() + "，学号是 " + studentId;
    }
    
    public void study() {
        System.out.println("学生正在学习");
    }
    
    public String getStudentId() {
        return studentId;
    }
}

// 4.3 继承 - Teacher类
class Teacher extends Person {
    private String subject;
    
    public Teacher(String name, int age, String subject) {
        super(name, age); // 调用父类构造方法
        this.subject = subject;
    }
    
    // 方法重写
    @Override
    public String getIntroduction() {
        return super.getIntroduction() + "，教授科目是 " + subject;
    }
    
    public void teach() {
        System.out.println("老师正在授课");
    }
    
    public String getSubject() {
        return subject;
    }
}

// 4.4 多态 - Calculator类
class Calculator {
    // 方法重载
    public int add(int a, int b) {
        return a + b;
    }
    
    public double add(double a, double b) {
        return a + b;
    }
    
    public int add(int a, int b, int c) {
        return a + b + c;
    }
}

// 4.4 多态 - Animal类及相关类
abstract class Animal {
    public abstract void makeSound();
}

class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("狗在汪汪叫");
    }
    
    public void wagTail() {
        System.out.println("狗在摇尾巴");
    }
}

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("猫在喵喵叫");
    }
}

// 4.5 抽象类和接口 - Shape抽象类
abstract class Shape {
    public abstract double calculateArea();
}

class Circle extends Shape {
    private double radius;
    
    public Circle(double radius) {
        this.radius = radius;
    }
    
    @Override
    public double calculateArea() {
        return Math.PI * radius * radius;
    }
}

class Rectangle extends Shape {
    private double width;
    private double height;
    
    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
    
    @Override
    public double calculateArea() {
        return width * height;
    }
}

// 4.5 抽象类和接口 - Flyable接口
interface Flyable {
    void fly();
}

class Bird implements Flyable {
    @Override
    public void fly() {
        System.out.println("鸟儿在飞翔");
    }
}

class Airplane implements Flyable {
    @Override
    public void fly() {
        System.out.println("飞机在飞行");
    }
}

// 4.5 Java 8+ 接口新特性 - Movable接口
interface Movable {
    void speedUp();
    
    // 默认方法
    default void move() {
        System.out.println("物体在移动");
    }
}

class Car implements Movable {
    @Override
    public void speedUp() {
        System.out.println("汽车加速");
    }
    
    // 可以选择重写默认方法
    @Override
    public void move() {
        System.out.println("汽车在行驶");
    }
}